package com.xiangxiao.rpan.authority.controller;

import com.xiangxiao.rpan.authority.constant.ResponseCode;
import com.xiangxiao.rpan.authority.constant.ResponseMessage;
import com.xiangxiao.rpan.authority.dto.JsonObject;
import com.xiangxiao.rpan.authority.dto.QueryUsersByTopicDto;
import com.xiangxiao.rpan.authority.dto.SaveTopicUserDto;
import com.xiangxiao.rpan.authority.exception.LoginException;
import com.xiangxiao.rpan.authority.service.LoginService;
import com.xiangxiao.rpan.authority.vo.TopicVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2024/7/2 16:14
 */
@Slf4j
@RestController
@RequestMapping("/topic")
public class TopicController {
  private static Logger logger = LoggerFactory.getLogger(TopicController.class);

  @Autowired
  private LoginService loginService;

  /**
   * {
   *    "topic": "alarm/file-upload"
   * }
   * */
  @PostMapping("/query/userList")
  public JsonObject queryUserIdsByTopic(@Valid @RequestBody QueryUsersByTopicDto ueryUsersByTopicDto) {
    try {
      List<String> userIds = loginService.queryUserIdsByTopic(ueryUsersByTopicDto);
      return new JsonObject(ResponseMessage.QUERY_USERIDS_BY_TOPIC_SUECCESS, ResponseCode.RESULT_SUCCESS, userIds);
    } catch (LoginException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.QUERY_USERIDS_BY_TOPIC_FAILED, ResponseCode.QUERY_USERIDS_BY_TOPIC_FAILED);
    }
  }

  @GetMapping("/getAll")
  public JsonObject getAllTopics() {
    try {
      List<TopicVo> vos = loginService.queryAllAlarmTopics();
      return new JsonObject(ResponseMessage.QUERY_ALL_TOPIC_SUECCESS, ResponseCode.RESULT_SUCCESS, vos);
    } catch (LoginException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.QUERY_ALL_TOPIC_FAILED, ResponseCode.QUERY_ALL_TOPIC_FAILED);
    }
  }

  /**
   * {"topic":"alarm/floder-upload","topicId":3,"userIds":[11]}
   * */
  @PostMapping("/save/userList")
  public JsonObject saveUserList(@Valid @RequestBody SaveTopicUserDto saveTopicUserDto) {
    try {
      List<Integer> rowIds = loginService.batchUpdateTopicUsers(saveTopicUserDto);
      return new JsonObject(ResponseMessage.UPDATE_RECIVED_USERS_BY_TOPICS_SUECCESS, ResponseCode.RESULT_SUCCESS, rowIds);
    } catch (LoginException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.UPDATE_RECIVED_USERS_BY_TOPICS_FAILED, ResponseCode.UPDATE_RECIVED_USERS_BY_TOPICS_FAILED);
    }
  }
}
